<?php
/*
 * @Descripttion: 管理后台登录
 * @version:
 * @Author: ewgof
 * @Date: 2019-10-29 18:14:02
 * @LastEditors: ewgof
 * @LastEditTime: 2020-10-25 16:50:08
 */
namespace app\http\controller\admin;

use cw\facade\Cache;


class Login extends Base
{
    /**
     * @name: login
     * @msg: 管理后台用户登录
     * @param $username string 用户名
     * @param $password string 密码
     * @return:
     */
    public function login($request)
    {
        $input = $request->post();
        $username = base64_decode($input['username']);
        $password = base64_decode($input['password']);
        if (empty($username) || empty($password)) {
            return api_json_return(2);
        }

        $password_err_num_key = 'admin:password_error_numbers:' . $username;
        if (Cache::has($password_err_num_key) && Cache::get($password_err_num_key) >= 5) {
            $msg = '用户登录失败：输入错误密码次数达到或者大于 5 次，账号已经被锁定。' . ' username => ' . $input['username'] . ', password => ' . $input['password'] . ', clientid => ' . $this->clientid;
            echo $msg . PHP_EOL;
            log_msg($msg, 'error', $this->log_filename_apperr);
            return api_json_return(10005);
        }

        $ad_user_info = $this->db->get('admins', ['id', 'username', 'password', 'status', 'picture'], ['username' => $username]);
        if (empty($ad_user_info)) {
            $msg = '登录错误：用户名不存在. username => '.$username . ', ad_user_info => ' . json_encode($ad_user_info);
            log_msg($msg, 'apperr', $this->log_filename_apperr);
            echo $msg . PHP_EOL;
            return api_json_return(20000);
        }

        if (!\password_verify($password, $ad_user_info['password'])) {
            if (Cache::has($password_err_num_key)) {
                if (Cache::get($password_err_num_key) < 5) {
                    Cache::inc($password_err_num_key);
                }
            } else {
                Cache::set($password_err_num_key, 1, 300);
            }
            $msg = '用户登录失败：输入错误密码。' . ' username => ' . $username . ', password => ' . $password . ', uid => ' . $ad_user_info['id'] . ', clientid => ' . $this->clientid;
            log_msg($msg, 'error', $this->log_filename_apperr);
            echo $msg . PHP_EOL;
            return api_json_return(10003);
        }

        if ($ad_user_info['status'] != 1) {
            return api_json_return(10004);
        }

        $up_aduser_data = array(
            'id' => $ad_user_info['id'],
            'token' => make_token(),
            'expire' => $this->t + ADMIN_TOKEN_EXPIRE,
            'created_at' => $this->t,
            'updated_at' => $this->t,
        );

        $info = $this->db->update('admins', $up_aduser_data, ['id' => $ad_user_info['id']])->rowCount();
        if ($info === 1) {
            unset($ad_user_info['password'], $ad_user_info['status']);
            $ad_user_info['token'] = $up_aduser_data['token'];
            $ad_user_info['expire'] = $up_aduser_data['expire'];
            return api_json_return(0, '', $ad_user_info);
        } else {
            $msg = '登录失败：更新用户表或者日志记录表失败。 username => '.$username.', info => '.$info;
            log_msg($msg, 'apperr', $this->log_filename_apperr);
            echo $msg . PHP_EOL;
            return api_json_return(1);
        }
    }


    public function logout($request)
    {
        $token = $request->header('token');

        $ad_user_info = $this->db->get('admins', ['id', 'username'], ['token' => $token]);

        if (empty($ad_user_info)) {
            return api_json_return(0);
        }
        $up_aduser_data = array(
            'token' => '',
            'expire' => 0,
            'updated_at' => $this->t,
        );
        $info = $this->db->update($up_aduser_data, ['id' => $ad_user_info['id']])->rowCount();
        if ($info === 1) {
            return api_json_return(0);
        } else {
            $msg = '退出登录失败：更新用户表失败。username => '.$ad_user_info['username'].', token => '.$token.', info => '.$info;
            log_msg($msg, 'apperr', $this->log_filename_apperr);
            echo $msg . PHP_EOL;
            return api_json_return(1);
        }
    }
}
